package com.bizfx.component.views
{
	import flash.events.Event;
	
	import com.bizfx.component.controls.ListItemSelectorColumn;
	import com.bizfx.component.controls.ListItemSelectorHeaderRenderer;
	import com.bizfx.events.ListItemSelectorHeaderClickEvent;
	import com.bizfx.utils.ObjectUtils;
	
	import mx.collections.IList;
	import mx.controls.CheckBox;
	import mx.controls.DataGrid;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.ClassFactory;
	import mx.core.IFactory;
	import mx.formatters.DateFormatter;
	import mx.formatters.NumberFormatter;
	
	public class BaseGridListView extends BaseListView implements IListView
	{
		private var dateFormatter:DateFormatter = new DateFormatter();
		
		private var numberFormatter:NumberFormatter = new NumberFormatter();
		
		private function get thisList():DataGrid {
			if (this.list != null) {
				return this.list as DataGrid;
			}
			return null;
		}
		
		override protected function createList():void {
			super.createList();
			if (this.list == null) {
				this.list = new DataGrid();
				this.list.percentHeight = 100;
				this.list.percentWidth = 100;
			}
			this.createListColumns();		
		}
		
		override protected function createListColumns():void {
			if (this.thisList) {
				this.thisList.columns = new Array();
				if (this.allowMultipleSelection) {
					var selectorColumn:ListItemSelectorColumn = new ListItemSelectorColumn();
					selectorColumn.sortable = false;
					selectorColumn.resizable = false;
					selectorColumn.draggable = false;
					selectorColumn.width = 28;
					selectorColumn.setStyle("paddingLeft", "6");
					selectorColumn.dataField = "__selector";
					selectorColumn.headerRenderer = new ClassFactory(ListItemSelectorHeaderRenderer);
					selectorColumn.itemRenderer = new ClassFactory(CheckBox);
					selectorColumn.addEventListener(ListItemSelectorHeaderClickEvent.ListItemSelectorHeaderClick, selectorHeaderHandler);
					thisList.columns = thisList.columns.concat(selectorColumn);
				}
				
				for (var i:int; i < this.columns.length; i++) {
					var column:ListViewColumn = this.columns.getItemAt(i) as ListViewColumn;
					if (column != null) {
						var item:DataGridColumn = new DataGridColumn();
						item.headerText = column.text?column.text:""; 
						item.dataField = column.dataField?column.dataField:"";
						if(column.labelFunction != null) {
							item.labelFunction = column.labelFunction;
						} else {
							item.labelFunction = defaultColumnDataFormat;
						}
						if (column.width > 0)
							item.width = column.width;
						thisList.columns = thisList.columns.concat(item);
					}
				}
			}
		}
		
		private function defaultColumnDataFormat(item:Object,column:DataGridColumn):String {
			var data:Object = ObjectUtils.getPropertyValue(item, column.dataField);
			
			if (data != null) {
				if (data is Date) {
					if (data.valueOf() == new Date(0, 0, 0).valueOf()) {
						return "";
					} else {
						dateFormatter.formatString="YYYY-MM-DD";
						return dateFormatter.format(data);						
					}
				} else if (data is Boolean) {
					return data?"是":"否";
				} else if (data is Number) {
//					dateFormatter.formatString="YYYY-MM-DD";
//					return dateFormatter.format(item[column.dataField]);
					data.toString();
				}
				return data.toString();	
			} else {
				return null;
			}
		}
	}
}